﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.ApplicationServices;
using System.Web.Hosting;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using Autofac;
using Autofac.Integration.Mvc;
using Autofac.Integration.WebApi;
using Microsoft.AspNet.SignalR;
using StackExchange.Profiling;
using StackExchange.Profiling.EntityFramework6;
using yiCommerce.App_Start;
using yiCommerce.Core;
using yiCommerce.Core.Data;
using yiCommerce.Data;
using yiCommerce.Hubs;
using yiCommerce.Service.Addresses;
using yiCommerce.Service.Authentication;
using yiCommerce.Service.Customers;
using yiCommerce.Service.Events;
using yiCommerce.Service.FriendShips;
using yiCommerce.Service.Loggings;
using yiCommerce.Service.Messages;
using yiCommerce.Service.Orders;
using yiCommerce.Service.Payments;
using yiCommerce.Service.Pictures;
using yiCommerce.Service.Products;
using yiCommerce.Service.Securitys;
using yiCommerce.Service.Shippings;
using yiCommerce.Service.ShoppingCarts;
using yiCommerce.Service.Shops;
using yiCommerce.Service.Statuses;
using yiCommerce.Service.Tradings;
using yiCommerce.Web.Framework.EmbeddedViews;
using Autofac.Integration.SignalR;
using AutofacDependencyResolver = Autofac.Integration.Mvc.AutofacDependencyResolver;

namespace yiCommerce
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            MiniProfilerEF6.Initialize();
            //initialize engine context
            var container = BuildContianer();
            EngineContext.Initialize(false, container);


            AreaRegistration.RegisterAllAreas();
            RouteTable.Routes.MapHubs();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            AuthConfig.RegisterAuth();


            var embeddedViewResolver = new EmbeddedViewResolver();
            var embeddedProvider = new EmbeddedViewVirtualPathProvider(embeddedViewResolver.GetEmbeddedViews());
            HostingEnvironment.RegisterVirtualPathProvider(embeddedProvider);
        }
        protected void Application_BeginRequest()
        {
            if (Request.IsLocal)
            {
                MiniProfiler.Start();
            }
        }

        protected void Application_EndRequest()
        {
            MiniProfiler.Stop();
        }


        private IContainer BuildContianer()
        {
            var builder = new ContainerBuilder();
            // Configure the container  
            //builder.RegisterHubs(Assembly.GetCallingAssembly());
           
            //db
            builder.RegisterGeneric(typeof(EfRepository<>)).As(typeof(IRepository<>)).InstancePerHttpRequest();
            builder.Register<DbContext>(c => new YiObjectContext()).InstancePerHttpRequest();


            //service
            builder.RegisterType<WebWorkContext>().As<IWorkContext>().InstancePerHttpRequest();
            builder.RegisterType<ProductService>().As<IProductService>();
            builder.RegisterType<ActivityService>().As<IActivityService>();
            builder.RegisterType<PermissionService>().As<IPermissionService>();
            builder.RegisterType<ProductSearchService>().As<IProductSearchService>();
            builder.RegisterType<PictureService>().As<IPictureService>();
            builder.RegisterType<ShoppingCartService>().As<IShoppingCartService>();
            builder.RegisterType<PermissionService>().As<IPermissionService>();
            builder.RegisterType<EventPublisher>().As<IEventPublisher>();
            builder.RegisterType<ProductAttributeParser>().As<IProductAttributeParser>();
            builder.RegisterType<CustomerService>().As<ICustomerService>();
            builder.RegisterType<ProductVariantAttributeFormatter>().As<IProductVariantAttributeFormatter>();
            builder.RegisterType<FormsAuthenticationService>().As<IAuthenticationService>();
            builder.RegisterType<Logger>().As<ILogger>();
            builder.RegisterType<OrderProcessingService>().As<IOrderProcessingService>();
            builder.RegisterType<OrderService>().As<IOrderService>();
            builder.RegisterType<PaymentService>().As<IPaymentService>();
            builder.RegisterType<AlipayPaymentMethod>().As<IPaymentMethod>().Named<IPaymentMethod>("AlipayPaymentMethod");
            builder.RegisterType<DummyPaymentMethod>().As<IPaymentMethod>().Named<IPaymentMethod>("DummyPaymentMethod");

            
            builder.RegisterType<AddressService>().As<IAddressService>();
            builder.RegisterType<ShippingService>().As<IShippingService>();
            builder.RegisterType<ProductAttributeFormatter>().As<IProductAttributeFormatter>();
            builder.RegisterType<OrderTotalCalculationService>().As<IOrderTotalCalculationService>();
            builder.RegisterType<TradingService>().As<ITradingService>();
            builder.RegisterType<EncryptionService>().As<IEncryptionService>();
            builder.RegisterType<CustomerRegistrationService>().As<ICustomerRegistrationService>();
            builder.RegisterType<FormsAuthenticationService>().As<IAuthenticationService>();
            builder.RegisterType<ShopService>().As<IShopService>();
            builder.RegisterType<StatusService>().As<IStatusService>().InstancePerHttpRequest();
            builder.RegisterType<FriendShipService>().As<IFriendShipService>().InstancePerHttpRequest();
            builder.RegisterType<MessageService>().As<IMessageService>().InstancePerHttpRequest();
            
            
            
            //controller
            var callingAssembly = Assembly.GetCallingAssembly();
            builder.RegisterControllers(callingAssembly);

            builder.RegisterApiControllers(callingAssembly);


            builder.RegisterType<StatusesHub>().ExternallyOwned();

            var container = builder.Build();
            //var resolver = new AutofacWebApiDependencyResolver(container);
            var resolver= new AutofacDependencyResolver(container);
            DependencyResolver.SetResolver(resolver);
            GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);

            GlobalHost.DependencyResolver = new Autofac.Integration.SignalR.AutofacDependencyResolver(container); ;
            return container;
        }
    }

    
}